/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.prashant.sqlduniya.editor;

import java.awt.Dimension;
import java.util.Collections;
import org.netbeans.api.settings.ConvertAsProperties;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.util.NbBundle.Messages;
import org.openide.util.lookup.AbstractLookup;
import org.openide.util.lookup.InstanceContent;
import org.openide.windows.TopComponent;
import org.prashant.sqlduniya.sqlmanager.SQLQuery;
import org.prashant.sqlduniya.sqlmanager.api.QueryChangeListener;

/**
 * Top component which displays something.
 */
@ConvertAsProperties(dtd = "-//org.prashant.sqlduniya.editor//QueryEditor//EN",
autostore = false)
@TopComponent.Description(preferredID = "QueryEditorTopComponent",
//iconBase="SET/PATH/TO/ICON/HERE", 
persistenceType = TopComponent.PERSISTENCE_ALWAYS)
@TopComponent.Registration(mode = "editor", openAtStartup = true)
@ActionID(category = "Window", id = "org.prashant.sqlduniya.editor.QueryEditorTopComponent")
@ActionReference(path = "Menu/Window" /*
 * , position = 333
 */)
@TopComponent.OpenActionRegistration(displayName = "#CTL_QueryEditorAction",
preferredID = "QueryEditorTopComponent")
@Messages({
    "CTL_QueryEditorAction=QueryEditor",
    "CTL_QueryEditorTopComponent=QueryEditor Window",
    "HINT_QueryEditorTopComponent=This is a QueryEditor window"
})
public final class QueryEditorTopComponent extends TopComponent implements QueryChangeListener {

    private InstanceContent content = new InstanceContent();

    public QueryEditorTopComponent() {
        initComponents();
        setName(Bundle.CTL_QueryEditorTopComponent());
        setToolTipText(Bundle.HINT_QueryEditorTopComponent());
        putClientProperty("print.printable", Boolean.TRUE);        
        
        associateLookup(new AbstractLookup(content));
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        queryPanel = new org.prashant.sqlduniya.editor.QueryPanel();

        setLayout(new java.awt.BorderLayout());
        add(queryPanel, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private org.prashant.sqlduniya.editor.QueryPanel queryPanel;
    // End of variables declaration//GEN-END:variables

    @Override
    public void componentOpened() {
        queryPanel.addQueryChangeListener(this);
    }

    @Override
    public void componentClosed() {
        queryPanel.removeQueryChangeListener(this);
    }

    void writeProperties(java.util.Properties p) {
        // better to version settings since initial version as advocated at
        // http://wiki.apidesign.org/wiki/PropertyFiles
        p.setProperty("version", "1.0");
        // TODO store your settings
    }

    void readProperties(java.util.Properties p) {
        String version = p.getProperty("version");
        // TODO read your settings according to their version
    }

    @Override
    public void queryChanged(SQLQuery sqlQuery) {
        content.set(Collections.singleton(sqlQuery), null);
    }
}
